/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.creator.logical.links.db;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author ~jo-MA-jo~
 */
public class TableObject {
    public String name;
    public int type;
    public Object content;
    public byte[] contentByte;

    public TableObject (int _type, Object _content){
        name = "";
        type = _type;
        content = _content;
        contentByte = toByteArray (_content);
    }

    public TableObject (String _name, int _type, Object _content){
        name = _name;
        type = _type;
        content = _content;
        contentByte = toByteArray (_content);
    }

    public static Object toObject (byte[] bytes)
        {
          Object obj = null;
          try {
            ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
            ObjectInputStream ois = new ObjectInputStream (bis);
            obj = ois.readObject();
          }
          catch (Exception ex) {
              ex.printStackTrace();
            //TODO: Handle the exception
          }
          return obj;
    }

    public static byte[] toByteArray (Object obj){
          byte[] bytes = null;
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            oos.close();
            bos.close();
            bytes = bos.toByteArray ();
          }
          catch (Exception ex) {
               ex.printStackTrace();
            //TODO: Handle the exception
          }
          return bytes;
    }
}
